package ru.tests;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * TODO: add description
 * Date: 22.08.11 21:34
 *
 * @author Alexander Troshanin
 */
public class GetSubMapFromTreeMapExample {

    public static void main(String[] args) {
        //create TreeMap object
        TreeMap treeMap = new TreeMap();

//add key value pairs to TreeMap
        treeMap.put("1", "One");
        treeMap.put("11", "One-One");
        treeMap.put("11112", "11112");
        treeMap.put("11112-as", "11112-as");
        treeMap.put("3", "Three");
        treeMap.put("2", "Two");
        treeMap.put("22", "Two-Two");
        treeMap.put("23", "Two-Three2");
        treeMap.put("5", "Five");
        treeMap.put("4", "Four");

/*
To get the sub Map from Java TreeMap use,
SortedMap subMap(int fromKey, int toKey) method of TreeMap class.

This method returns portion of the TreeMap whose keys range from
fromKey (inclusive) to toKey(exclusive).

Please note that, the SortedMap returned by this method is backed by
the original TreeMap. So any changes made to SortedMap will be
reflected back to original TreeMap.
*/

        Map.Entry lastEntry = treeMap.lastEntry();

        Map.Entry entry = treeMap.floorEntry("11112");


        System.out.println(treeMap.floorEntry("11112"));
        System.out.println(treeMap.subMap(entry.getKey(), true, lastEntry.getKey(), true));


        SortedMap sortedMap = treeMap.subMap("11", "5");

        System.out.println("SortedMap Contains : " + sortedMap);
    }

}


